package com.cloudfast.platform.app.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cloudfast.platform.app.AppClient;
import com.cloudfast.platform.app.dao.AppDao;

/**
 * app
 *
 * @author liuyw
 * @date 2022年9月11日
 */
@Service
@Transactional(readOnly = true)
public class AppService {
    protected static final Logger logger = LoggerFactory.getLogger(Logger.class);

    @Autowired
    private AppDao appDao;

    /**
     * 查询根据applabel
     *
     * @param appLabel
     * @return
     */
    public AppClient findOneByAppLabel(String appLabel, String corpId, String appType) {
        return appDao.findOneByAppLabel(appLabel, corpId, appType);
    }

    /**
     * 判断appLabel是否存在
     *
     * @param appLabel
     * @param id
     * @return
     */
    public int checkAppLabelExist(String appLabel, Long id) {
        StringBuilder hql = new StringBuilder("from AppClient where 1=1 ");
        Map<String, Object> params = new HashMap<>();
        if (id != null) {
            params.put("id", id);
            hql.append(" and id != :id");
        }
        hql.append(" and appLabel = :appLabel");
        params.put("appLabel", appLabel);

        List<AppClient> list = this.appDao.findList(hql.toString(), params);
        if (list != null && !list.isEmpty()) {
            return list.size();
        }
        return 0;
    }
}
